package com.yanqu.road.server.manger.banquet;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.logic.bussiness.config.BanquetConfigBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 宴会
 */
public class BanquetConfigMgr extends TempMgr {

	private static Map<Integer, BanquetInfo> banquetInfoMap;

	private static Map<Integer, BanquetGiftInfo> banquetGiftInfoMap;

	private static RandomHelper threadSafeRandom = new RandomHelper();

	//本服盛宴人气礼奖励
	//1-10-101000=1;110091=1|2-20-101000=1;110091=1
	public static Map<Integer,List<String>> getBanquetFeastRewardMap(){
		Map<Integer,List<String>> res = new ConcurrentHashMap<>();
		List<String> levelStringList = StringUtils.stringToStringList(GameConfig.BANQUET_FEAST_POPULARITY_GIFT,"\\|");
		for(String levelString : levelStringList){
			List<String> infoList = StringUtils.stringToStringList(levelString,"-");
			res.put(Integer.valueOf(infoList.get(0)),infoList);
		}
		return res;
	}

	//判斷是不是要同步本服盛宴的人氣禮
	public static boolean isSendBanquetFeastRewardPoint(int userCount){
		Map<Integer,List<String>> rewardInfoMap = BanquetConfigMgr.getBanquetFeastRewardMap();
		if(userCount > Integer.valueOf(rewardInfoMap.get(rewardInfoMap.size()).get(1)) + 3){
			return false;
		}
		for(List<String> infoMap : rewardInfoMap.values()){
			if(userCount == Integer.valueOf(infoMap.get(1))){
				return true;
			}
		}
		return false;
	}

	public static boolean isSendCrossBanquetPopRewardPoint(int userCount){
		Map<Integer,List<String>> rewardInfoMap = BanquetConfigMgr.getCrossBanquetPopRewardMap();
		if(userCount > Integer.valueOf(rewardInfoMap.get(rewardInfoMap.size()).get(1)) + 5){
			return false;
		}
		for(List<String> infoMap : rewardInfoMap.values()){
			if(userCount == Integer.valueOf(infoMap.get(1))){
				return true;
			}
		}
		return false;
	}


	//跨服盛宴人气礼奖励
	//1-10-101000=1;110091=1|2-20-101000=1;110091=1
	public static Map<Integer,List<String>> getCrossBanquetPopRewardMap(){
		Map<Integer,List<String>> res = new ConcurrentHashMap<>();
		List<String> levelStringList = StringUtils.stringToStringList(GameConfig.BANQUET_CROSS_POPULARITY_GIFT,"\\|");
		for(String levelString : levelStringList){
			List<String> infoList = StringUtils.stringToStringList(levelString,"-");
			res.put(Integer.valueOf(infoList.get(0)),infoList);
		}
		return res;
	}


	//获取增加美女亲密度
	public static int getBeautyAddValue(int banquetInfoId){
		BanquetInfo banquetInfo = getBanquetInfo(banquetInfoId);
		if(!"0".equals(banquetInfo.getParams())){
			List<String> paramsList = StringUtils.stringToStringList(banquetInfo.getParams(),"\\|");
			return Integer.valueOf(paramsList.get(0));
		}
		return 0;
	}

	//获取子嗣宴的子嗣加成赚速千分比
	public static int getChildAddRatio(int banquetInfoId){
		BanquetInfo banquetInfo = getBanquetInfo(banquetInfoId);
		if(!"0".equals(banquetInfo.getParams())){
			List<String> paramsList = StringUtils.stringToStringList(banquetInfo.getParams(),"\\|");
			return Integer.valueOf(paramsList.get(0));
		}
		return 0;
	}

	//获取对应类型的日志要用到的id字符串in
	public static String getBanquetIdIn(int banquetType){
		List<Integer> banquetIdList = new ArrayList<>();
		for(BanquetInfo info : banquetInfoMap.values()){
			if(isTypeBanquet(banquetType,info)){
				banquetIdList.add(info.getId());
			}
		}
		return StringUtils.listToString(banquetIdList,",");
	}

	//获取全服盛宴的对应位置的对应奖励
	public static String getJoinReward(int pos,int giftId){
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		if(banquetGiftInfo != null && !"0".equals(banquetGiftInfo.getJoinReward())){
			int max = -1;
			String reward = "";
			List<String> rewardList = StringUtils.stringToStringList(banquetGiftInfo.getJoinReward(),"\\|");
			for(String rewardInfo : rewardList){
				List<String> posRewardList = StringUtils.stringToStringList(rewardInfo,":");
				//最小位置以上的简历
				int paramPosMin = Integer.valueOf(posRewardList.get(0));
				if(pos >= paramPosMin && paramPosMin > max){
					max = paramPosMin;
					reward = posRewardList.get(1);
				}
			}
			return reward;
		}
		return "";
	}

	//宴会结束奖励
	public static String getEndReward(int pos,int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		if(banquetInfo != null && !"0".equals(banquetInfo.getEndReward())){
			int max = -1;
			String reward = "";
			List<String> rewardList = StringUtils.stringToStringList(banquetInfo.getEndReward(),"\\|");
			for(String rewardInfo : rewardList){
				List<String> posRewardList = StringUtils.stringToStringList(rewardInfo,":");
				//最小位置以上的简历
				int paramPosMin = Integer.valueOf(posRewardList.get(0));
				if(pos >= paramPosMin && paramPosMin > max){
					max = paramPosMin;
					reward = posRewardList.get(1);
				}
			}
			return reward;
		}
		return "";
	}

	//根据对应三种类型获取宴会信息
	private static boolean isTypeBanquet(int banquetType,BanquetInfo info){
		if(banquetType == eBanquetType.ServerNormalBanquet.getValue()){
			return isServerNormalBanquet(info.getId());
		}else if(banquetType == eBanquetType.ServerFeastBanquet.getValue()){
			return isServerFeastBanquet(info.getId());
		}else if(banquetType == eBanquetType.CrossBanquet.getValue()){
			return isCrossBanquet(info.getId());
		}else if(banquetType == eBanquetType.CrossUserBanquet.getValue()){
			return isCrossUserBanquet(info.getId());
		}else if(banquetType == eBanquetType.CrossMogulBirthdayBanquet.getValue()){

		}
		return false;
	}

	//判断是不是本服普通宴会，类型1是1和2 的
	public static boolean isServerNormalBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		if(banquetInfo.getType1() == 1 || banquetInfo.getType1() == 2){
			return true;
		}
		return false;
	}

	//判断宴会是不是本服盛宴
	public static boolean isServerFeastBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 3;
	}

	//判断宴会是不是跨服宴会
	public static boolean isCrossBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 4;
	}

	//判断宴会是否是生辰宴会
	public static boolean isCrossMogulBirthdayBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == eBanquetType.CrossMogulBirthdayBanquet.getValue();
	}

    // 判断宴会是不是百福宴会
    public static boolean isCrossGoodBanquet(int banquetInfoId){
        BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
        return banquetInfo.getType1() == 6;
    }

	//判断宴会是不是跨服私人宴会
	public static boolean isCrossUserBanquet(int banquetInfoId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		return banquetInfo.getType1() == 5;
	}


	//两种宴会类型是不是同一个，判断能不能同时开启的
	public static boolean isTheSameType(int banquetInfoId1,int banquetInfoId2){
		BanquetInfo banquetInfo1 = banquetInfoMap.get(banquetInfoId1);
		BanquetInfo banquetInfo2 = banquetInfoMap.get(banquetInfoId2);
		if(banquetInfo1.getType1() == banquetInfo2.getType1()){
			return true;
		}
		//类型1和类型2都是私人宴会，只能开一个
		if((banquetInfo1.getType1() == 1 || banquetInfo1.getType1() == 2) && (banquetInfo2.getType1() == 1 || banquetInfo2.getType1() == 2)){
			return true;
		}
		return false;
	}


	//是不是请帖宴
	public static boolean isBanquetInvitation(int banquetInfoId){
		if(banquetInfoMap.get(banquetInfoId).getType1() == 2){
			return true;
		}
		return false;
	}

	//判断当前职业是不是成年会生成举办宴会的帖子
	public static boolean isChildInvitationOccupation(int childOccupation){
		List<Integer> occupationInvList = StringUtils.stringToIntegerList(GameConfig.CHILD_OCCUPATION_BANQUET_INVITATION,";");
		for(Integer occupation : occupationInvList){
			if(occupation == childOccupation){
				return true;
			}
		}
		return false;
	}

	public static BanquetInfo getBanquetInfo(int banquetInfoId){
		return banquetInfoMap.get(banquetInfoId);
	}

	public static BanquetGiftInfo getBanquetGiftInfo(int banquetInfoId,int giftId){
		BanquetInfo banquetInfo = banquetInfoMap.get(banquetInfoId);
		BanquetGiftInfo banquetGiftInfo = banquetGiftInfoMap.get(giftId);
		if(banquetInfo == null || banquetGiftInfo == null){
			return null;
		}
		if(banquetInfo.getType1() != banquetGiftInfo.getType1() || banquetInfo.getType2() != banquetGiftInfo.getType2()){
			return null;
		}
		return banquetGiftInfo;
	}


	public static boolean isBanquetConsumeContain(int type1,String consume){
		Property consumePro = PropertyHelper.parseStringToProperty(consume);
		for(BanquetGiftInfo info : banquetGiftInfoMap.values()){
			Property giftConsumPro = PropertyHelper.parseStringToProperty(info.getJoinConsume());
			if(info.getType1() == type1 && PropertyHelper.isTheSameGood(consumePro,giftConsumPro)){
				return true;
			}
		}
		return false;
	}



	//获取请帖的过期时间
	public static int getInvitationOverTime(int type){
		List<String> typeStringList = StringUtils.stringToStringList(GameConfig.BANQUET_INVITATION_OVERTIME,";");
		for(String typeInfo : typeStringList){
			List<Integer> typeInfoList = StringUtils.stringToIntegerList(typeInfo,"=");
			if(type == typeInfoList.get(0)){
				return typeInfoList.get(1);
			}
		}
		return 0;
	}

	//机器人触发条件
	public static Map<Integer,List<Integer>> getRobotCondtion(){
		Map<Integer,List<Integer>> res = new ConcurrentHashMap<>();
		List<String> tempList = StringUtils.stringToStringList(GameConfig.BANQUET_ROBOT_CONDITION,";");
		for(String process : tempList){
			List<String> processList = StringUtils.stringToStringList(process,":");
			res.put(Integer.valueOf(processList.get(0)),StringUtils.stringToIntegerList(processList.get(1),"-"));
		}
		return res;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	@Override
	public boolean reloadConfig() {
		banquetInfoMap = BanquetConfigBussiness.getBanquetInfoMap();
		banquetGiftInfoMap = BanquetConfigBussiness.getBanquetGiftInfoMap();

		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	public static Map<Integer, BanquetGiftInfo> getBanquetGiftInfoMap() {
		return banquetGiftInfoMap;
	}

}
